﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GSync
{
    public partial class Settings : Form
    {
        string passStub = "dontknow";
        public Settings()
        {
            InitializeComponent();

            FillSyncPeriods();


            LoadIntoForm();

        }

        private void LoadIntoForm()
        {
            this.AccountTextBox.Text = GSyncFactory.Instance.SettingsManager.ServerAccountName;
            this.PasswordTextBox.Text = passStub;

            this.SyncCalendarCheckBox.Checked = GSyncFactory.Instance.SettingsManager.SyncCalendar;
            this.SyncContactsCeckBox.Checked = GSyncFactory.Instance.SettingsManager.SyncContacts;

            foreach (KeyValuePair<string, TimeSpan> pair in this.SyncPeriodComboBox.Items)
            {
                if (pair.Value == GSyncFactory.Instance.SettingsManager.SyncPeriod)
                {
                    this.SyncPeriodComboBox.SelectedItem = pair;
                }
            }
            
        }

        protected void FillSyncPeriods()
        {
            Dictionary<string, TimeSpan> vals = new Dictionary<string, TimeSpan>(5);

            vals.Add("1 hour", new TimeSpan(1, 0, 0));
            vals.Add("3 hours", new TimeSpan(3, 0, 0));
            vals.Add("6 hours", new TimeSpan(6, 0, 0));
            vals.Add("12 hours", new TimeSpan(12, 0, 0));
            vals.Add("1 day", new TimeSpan(1,0, 0, 0));

            SyncPeriodComboBox.Items.Clear();
            SyncPeriodComboBox.DisplayMember= "Key";
            SyncPeriodComboBox.ValueMember = "Value";
            foreach( KeyValuePair<string, TimeSpan> pair in vals )
            {
                SyncPeriodComboBox.Items.Add(pair);
            }
            
       
            
        }

        private void menuItemSave_Click(object sender, EventArgs e)
        {

            GSyncFactory.Instance.SettingsManager.ServerAccountName = AccountTextBox.Text;
            if( !PasswordTextBox.Text.Equals( passStub ))
                GSyncFactory.Instance.SettingsManager.ServerPassword = PasswordTextBox.Text;
            GSyncFactory.Instance.SettingsManager.SyncContacts = SyncContactsCeckBox.Checked;
            GSyncFactory.Instance.SettingsManager.SyncCalendar = SyncCalendarCheckBox.Checked;

            GSyncFactory.Instance.SettingsManager.SyncPeriod = ((KeyValuePair<string, TimeSpan>)this.SyncPeriodComboBox.SelectedItem).Value;
            GSyncFactory.Instance.SettingsManager.Save();
            this.Close();
        }

        private void menuItemCancel_Click(object sender, EventArgs e)
        {
            GSyncFactory.Instance.SettingsManager.Load();
            this.Close();
        }

        private void bnTest_Click(object sender, EventArgs e)
        {

            if (!GServerUtil.TestGmailAccount(AccountTextBox.Text, PasswordTextBox.Text))
            {
                MessageBox.Show("Failed to access your\r\nGMail account.\r\nPlease check credentials\r\nand network connectivity.");
                SyncContactsCeckBox.Checked = false;
            }
            else
            {
                MessageBox.Show("Successfully connected to GMail account.");
                SyncContactsCeckBox.Checked = true;
            }

            if (!GServerUtil.TestCalendarAccount(AccountTextBox.Text, PasswordTextBox.Text))
            {
                MessageBox.Show("Failed to access your\r\nGoogle calendar account.\r\nPlease check credentials\r\nand network connectivity.");
                SyncCalendarCheckBox.Checked = false;
            }
            else
            {
                MessageBox.Show("Successfully connected to\r\nGoogle calendar account.");
                SyncCalendarCheckBox.Checked = true;
            }
        }

        private void AccountTextBox_LostFocus(object sender, EventArgs e)
        {
            if (!AccountTextBox.Text.Contains("@"))
            {
                AccountTextBox.Text += "@gmail.com";
            }
        }

    }
}